package com.cloud.wms.ui.inside.presenter;


import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.inside.contract.RevertContact;
import com.cloud.wms.ui.inside.entity.RevertBatchEntity;
import com.cloud.wms.ui.inside.entity.RevertEntity;
import com.cloud.wms.ui.main.entity.CommodityEntity;
import com.cloud.wms.ui.main.entity.PosEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.functions.Function;

/**
 * Created by Chen on 2021/1/5.
 * Description：还货
 */
public class RevertPresenter extends BaseRxPresenter<RevertContact.View> implements RevertContact.Presenter {

    public static final int GET_REVERT_COMMODITY = 0x100;
    public static final int GET_REVERT_POS = 0x101;
    public static final int GET_REVERT_AMOUNT = 0x102;
    public static final int SUBMIT_REVERT_AMOUNT = 0x103;
    public static final int GET_REVERT_COMMODITY_AMOUNT = 0x104;
    public static final int CHECK_REVERT_COMMODITY = 0x105;
    public static final int GET_POS_COMMODITY = 0x106;

    @Inject
    public RevertPresenter() {
    }

    /**
     * 加载需还货商品信息
     */
    @Override
    public void loadRevertCommodity() {
        Params params = Params.newInstance(Params.Type.MEMBER);
        FineExApplication
                .component()
                .http()
                .loadRevertCommodity(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<List<CommodityEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(List<CommodityEntity> info) {
                        if (info == null || info.isEmpty())
                            mView.showEmpty();
                        else {
                            mView.onSuccess(MessageCreator.createMessage(info, GET_REVERT_COMMODITY));
                            int waitAmount = 0;
                            for (int i = 0; i < info.size(); i++) {
                                waitAmount += info.get(i).getAmount();
                            }
                            mView.onSuccess(MessageCreator.createMessage(waitAmount, GET_REVERT_COMMODITY_AMOUNT));
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                        mView.showError();
                    }
                });
    }

    /**
     * 加载可还货上架推荐库位
     *
     * @param commodityID
     */
    @Override
    public void loadCommoditySortPos(long commodityID, int type) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("CommodityID", commodityID);
        params.put("type", type);//1 取消订单 2 多捡
        FineExApplication
                .component()
                .http()
                .loadCommoditySortPos(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<List<PosEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(List<PosEntity> info) {
                        mView.onSuccess(MessageCreator.createMessage(info, GET_REVERT_POS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 多拣情况下校验商品
     *
     * @param barCode
     */
    @Override
    public void checkCommodity(String barCode) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("CommodityCode", barCode);
        FineExApplication
                .component()
                .http()
                .loadRevertCommodity(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<List<CommodityEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(List<CommodityEntity> info) {
                        if (info == null || info.isEmpty())
                            mView.info("未查询到符合条件的商品数据");
                        else {
                            mView.onSuccess(MessageCreator.createMessage(info.get(0), CHECK_REVERT_COMMODITY));
                        }
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
//                        mView.showError();
                    }
                });
    }

    /**
     * 加载该商品可上架数和批次信息
     *
     * @param commodityID
     * @param posCode
     */
    @Override
    public void upShelfPosAmount(long commodityID, String posCode) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("CommodityID", commodityID);
        params.put("PosCode", posCode);
        FineExApplication
                .component()
                .http()
                .upShelfPosAmount(params)
                .map(new ResponseWrapper<>())
                .map(new Function<ArrayList<RevertBatchEntity>, RevertEntity>() {
                    @Override
                    public RevertEntity apply(ArrayList<RevertBatchEntity> revertBatchEntities) throws Exception {
                        RevertEntity revertEntity = new RevertEntity();
                        revertEntity.setBatchList(revertBatchEntities);
                        int totalAmount = 0;
                        for (int i = 0; i < revertBatchEntities.size(); i++) {
                            totalAmount = totalAmount + revertBatchEntities.get(i).getAmount();
                        }
                        revertEntity.setTotalAmount(totalAmount);
                        return revertEntity;
                    }
                })
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<RevertEntity>(mView.provideContext()) {
                    @Override
                    public void onSuccess(RevertEntity info) {
                        mView.onSuccess(MessageCreator.createMessage(info, GET_REVERT_AMOUNT));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }

    /**
     * 异常或者多拣加载库位商品信息
     *
     * @param commodityID
     * @param posCode
     */
    @Override
    public void loadPosCommodity(long commodityID, String posCode, int type) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("CommodityID", commodityID);
        params.put("type", 2);//1 取消订单 2 多捡
        FineExApplication
                .component()
                .http()
                .morePickCommodityPos(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<List<CommodityEntity>>(mView.provideContext()) {
                    @Override
                    public void onSuccess(List<CommodityEntity> info) {
                        mView.onSuccess(MessageCreator.createMessage(info, GET_POS_COMMODITY));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });

//        Params params = new Params();
//        Observable.timer(500, TimeUnit.MILLISECONDS)
//                .map((Function<Long, List<CommodityEntity>>) aLong -> {
//                    ArrayList<CommodityEntity> beans = new ArrayList<>();
//                    for (int i = 0; i < 10; i++) {
//                        CommodityEntity commodityBean = new CommodityEntity();
//                        commodityBean.setCommodityID((int) (Math.random() * 1000));
//                        int barCode = (int) (Math.random() * 1000);
//                        commodityBean.setCommodityName("测试商品" + barCode);
//                        commodityBean.setBarCode(barCode + "");
//                        commodityBean.setAmount((int) (Math.random() * 10));
//                        commodityBean.setCommodityCodeList(null);
//
//                        beans.add(commodityBean);
//                    }
//                    return beans;
//                })
////        FineExApplication
////                .component()
////                .http()
////                .containerRelease(params)
////                .map(new ResponseWrapper<>())
//                .compose(mView.toLifecycle())
//                .compose(RxUtils.threadSwitch())
//                .subscribe(new LoadingObserver<List<CommodityEntity>>(mView.provideContext()) {
//                    @Override
//                    public void onSuccess(List<CommodityEntity> info) {
//                        mView.onSuccess(MessageCreator.createMessage(info, GET_POS_COMMODITY));
//                    }
//
//                    @Override
//                    public void onError(String msg) {
//                        mView.error(msg);
//                    }
//                });
    }

    /**
     * 确认还货上架
     *
     * @param commodityID
     * @param posID
     * @param Amount
     */
    @Override
    public void confirmUpShelf(long commodityID, String posID, int Amount, int type, List<RevertBatchEntity> selectBatchList) {
        Params params = Params.newInstance(Params.Type.MEMBER);
        params.put("CommodityID", commodityID);
        params.put("posID", posID);
        params.put("createBy", FineExApplication.component().loginCache().getUserInfo().getUserID());
        params.put("returnType", type);//1 取订还货 2多捡还货
        params.put("productBatchInfo", selectBatchList);

        FineExApplication
                .component()
                .http()
                .confirmUpShelf(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<Object>(mView.provideContext()) {
                    @Override
                    public void onSuccess(Object info) {
                        mView.onSuccess(MessageCreator.createMessage(Amount, SUBMIT_REVERT_AMOUNT));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
